//
//  YCLTabBarController.swift
//  新浪微博01
//
//  Created by yaochunlin on 16/4/8.
//  Copyright © 2016年 yaochunlin. All rights reserved.
//

import UIKit
import SVProgressHUD

class YCLTabBarController: UITabBarController {

    override func viewDidLoad() {
        
        //UITabBarController的tabBar属性是只读的，我们采用kvc给属性赋值
        let YCLtabBar = YCLTabBar()
        YCLtabBar.composeClosure = {
        
//            print("加号按钮被点击")
            
            if !YCLUserAccountViewModel.sharedUserAccountViewModel.isLogin {
                
                SVProgressHUD.showErrorWithStatus("请先登录")
                return
            }
            
            //modal出一个带导航控制器的普通控制器
            let composeVC = YCLComposeViewController()
            let navVC = UINavigationController.init(rootViewController: composeVC)

            self.presentViewController(navVC, animated: true, completion: nil)
            
        }
        
        self.setValue(YCLtabBar, forKeyPath: "tabBar")
      
        setupChildViewControllers()


    }
    
    //MARK:私有方法 
    //设置所有子控制器
    private func setupChildViewControllers() {
    
        //第一个模块为home模块
//        let homeVC = YCLHomeViewController()
//        homeVC.tabBarItem.image = UIImage(named: "tabbar_home")
//        homeVC.title = "首页"
//        
//        //设置TabBarController的根控制器
//        addChildViewController( UINavigationController(rootViewController: homeVC))
        setupChildViewController( YCLHomeViewController(), imageName: "tabbar_home", title: "首页")
        
        //第二个模块为信息模块
//        let messageVC = YCLMessageViewController()
//        messageVC.tabBarItem.image = UIImage(named: "tabbar_message_center")
//        messageVC.title = "信息"
//        
//        //设置TabBarController的根控制器
//        addChildViewController( UINavigationController(rootViewController: messageVC))
        setupChildViewController(  YCLMessageViewController(), imageName: "tabbar_message_center", title: "信息")
        
        //第三个模块为发现模块
//        let discoverVC = YCLDiscoverViewController()
//        discoverVC.tabBarItem.image = UIImage(named: "tabbar_discover")
//        discoverVC.title = "发现"
//        
//        //设置TabBarController的根控制器
//        addChildViewController( UINavigationController(rootViewController: discoverVC))
        setupChildViewController(  YCLDiscoverViewController(), imageName: "tabbar_discover", title: "发现")
        
        //第四个模块为我的模块
//        let profileVC = YCLProfileViewController()
//        profileVC.tabBarItem.image = UIImage(named: "tabbar_profile")
//        profileVC.title = "我的"
//        
//        //设置TabBarController的根控制器
//        addChildViewController( UINavigationController(rootViewController: profileVC))
        setupChildViewController(  YCLProfileViewController(), imageName: "tabbar_profile", title: "我的")
        
    }
    
    //设置单个子控制器
    private func setupChildViewController(childVC: UIViewController,imageName:String,title:String) {
    
        childVC.tabBarItem.image = UIImage(named: imageName)
        childVC.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        childVC.title = title
        
        //设置标题颜色
        childVC.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected)
        
        //设置TabBarController的根控制器
        addChildViewController( UINavigationController(rootViewController: childVC))
        
    }
    
    

}
